from django_redis import get_redis_connection
from django.http import HttpResponse
from meiduo_hmh.libs.captcha.captcha import captcha

def image_code(request,image_id):
    # 1.获取参数
    redis_store = get_redis_connection('image_codes')
    # 2.校验参数,cur_id
    if not image_id:
        return "图片验证码编号不能为空"
    # 4.生成图片验证码,并存储到redis中
    name, text, image_data = captcha.generate_captcha()
    try:
        #参数格式: key, value, time
        redis_store.set("image_code:%s"%image_id,text,60)
    except :
        raise HttpResponse('图片验证码存储存数失败')
    # 5.返回图片验证码
    response = HttpResponse(image_data,content_type="image/png")
    # response.headers["Content-Type"] = "image/png"
    return response